package org.mame.graphics;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mame.builders.BuildMediaReport;
import org.mame.builders.BuildRomConfigs;
import org.mame.builders.UpdateROMConfig;
import org.mame.util.XMLProcessor;

public class PropertySelector extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger log = LogManager.getLogger(PropertySelector.class.getName()); 
	private final JPanel contentPanel = new JPanel();
	private JTextField txtPropertyFile;
	private File propertyFile;
	private JTextField txtConfigFile;
	private File configFile;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			PropertySelector dialog = new PropertySelector();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public PropertySelector() {
		super(new DummyFrame("MAME Java Utility"));
		propertyFile = null;
		setBounds(100, 100, 625, 320);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblPropertyFile = new JLabel("Property File");
			lblPropertyFile.setBounds(10, 16, 61, 14);
			contentPanel.add(lblPropertyFile);
		}
		{
			txtPropertyFile = new JTextField();
			txtPropertyFile.setBounds(121, 11, 303, 25);
			contentPanel.add(txtPropertyFile);
			txtPropertyFile.setColumns(10);
		}
		{
			JButton btnNewButton = new JButton("Browse");
			btnNewButton.setBounds(434, 12, 79, 23);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					JFileChooser fileDialog = new JFileChooser();
					
					fileDialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
					
					int returnVal = fileDialog.showOpenDialog(PropertySelector.this);
					
					if(returnVal == JFileChooser.APPROVE_OPTION){
						propertyFile = fileDialog.getSelectedFile();
						txtPropertyFile.setText(propertyFile.getAbsolutePath());
						
					}
				}
			});
			contentPanel.add(btnNewButton);
		}
		
		JButton btnCreateRomConfig = new JButton("Create ROM Config XML");
		btnCreateRomConfig.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(txtPropertyFile.getText().length() > 0){
					int flag = BuildRomConfigs.buildConfig(new String[]{txtPropertyFile.getText()});
					if (flag == 0){
						JOptionPane.showMessageDialog(PropertySelector.this, "File created successfully", "", JOptionPane.INFORMATION_MESSAGE);
					} else {
						JOptionPane.showMessageDialog(PropertySelector.this, "Failure, see log file for more details", "", JOptionPane.ERROR_MESSAGE);
					}
				} else {
					JOptionPane.showMessageDialog(PropertySelector.this, "Must select a property file", "", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnCreateRomConfig.setBounds(82, 119, 181, 23);
		contentPanel.add(btnCreateRomConfig);
		{
			JButton btnCreateMediaReport = new JButton("Create Media Report");
			btnCreateMediaReport.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(txtPropertyFile.getText().length() > 0){
						int flag = BuildMediaReport.buildReport(new String[]{txtPropertyFile.getText()});
						if (flag == 0){
							JOptionPane.showMessageDialog(PropertySelector.this, "Report created successfully", "", JOptionPane.INFORMATION_MESSAGE);
						} else {
							JOptionPane.showMessageDialog(PropertySelector.this, "Failure, see log file for more details", "", JOptionPane.ERROR_MESSAGE);
						}
					}else{
						JOptionPane.showMessageDialog(PropertySelector.this, "Must select a property file", "", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			btnCreateMediaReport.setBounds(283, 119, 141, 23);
			contentPanel.add(btnCreateMediaReport);
		}
		
		JButton btnDomdocTest = new JButton("DOMDoc Test");
		btnDomdocTest.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String propFileStr = txtPropertyFile.getText();
				String configFileStr = txtConfigFile.getText();
				
				log.debug("Passing property file " + propFileStr);
				log.debug("Passing config file " + configFileStr);
				
				int flag = UpdateROMConfig.updateConfigFile(propFileStr, configFileStr, true);
				
				if (flag == 0){
					JOptionPane.showMessageDialog(PropertySelector.this, "Configuration file updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(PropertySelector.this, "Failure, see log file for more details", "", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnDomdocTest.setBounds(82, 169, 181, 23);
		contentPanel.add(btnDomdocTest);
		
		txtConfigFile = new JTextField();
		txtConfigFile.setBounds(121, 57, 303, 20);
		contentPanel.add(txtConfigFile);
		txtConfigFile.setColumns(10);
		
		JLabel lblConfigFile = new JLabel("Config File");
		lblConfigFile.setBounds(10, 60, 85, 14);
		contentPanel.add(lblConfigFile);
		
		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileDialog = new JFileChooser();
				
				fileDialog.setFileSelectionMode(JFileChooser.FILES_ONLY);
				
				int returnVal = fileDialog.showOpenDialog(PropertySelector.this);
				
				if(returnVal == JFileChooser.APPROVE_OPTION){
					configFile = fileDialog.getSelectedFile();
					txtConfigFile.setText(configFile.getAbsolutePath());
					
				}
			}
		});
		btnBrowse.setBounds(434, 56, 79, 23);
		contentPanel.add(btnBrowse);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton cancelButton = new JButton("Close");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						((DummyFrame)PropertySelector.this.getParent()).close();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}

class DummyFrame extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	DummyFrame(String title) {
        super(title);
        setUndecorated(true);
        setVisible(true);
        setLocationRelativeTo(null);
        //setIconImages((java.util.List<? extends Image>) iconImages);
    }
	
	public void close(){
		System.exit(1);
	}
}